import sys
import os
import re

def same_command(bucket):
    os.system('cd /home')
    os.system('sudo apt update -y')
    os.system('sudo apt install nvme-cli -y')
    os.system('sudo apt install git -y')
    os.system('sudo apt install jq -y')
    os.system('sudo apt install python3 -y')

    nvme = """nvme list --output-format=json | jq -r '.Devices' | jq -r 'map(select(.ModelNumber=="Amazon EC2 NVMe Instance Storage")) | .[].DevicePath'"""
    nvme_lists = os.popen(nvme).read()

    #print(nvme_lists.split())
    for i,block in enumerate(nvme_lists.split()):
        names=block.split("/");
        name=names[2]
        #os.system('sudo mkfs -t xfs -f '+ str(block))
        os.system('sudo mkfs.ext4 '+ str(block))
        # mkfs.ext4
        os.system('sudo mkdir /home/' + name)
        # os.system('sudo mkdir /home/chia' + str(i) + '/tmp')
        os.system('sudo mount '+str(block) +' /home/'+name)
        # os.system('df -h')

    #装goofys
    os.system('wget https://github.com/kahing/goofys/releases/latest/download/goofys')

    os.system('chmod +x /home/*')
    os.system('chmod +x ./goofys')

    os.system('sudo mkdir /home/chiap')
    # #提前给ec2 绑定 具有s3权限的iam role
    # instance_info = os.popen("ec2-metadata -i").read() #instance-id: i-00388231727f8b402
    # instance_id = instance_info[instance_info.find('i-')+2:] #'00388231727f8b402'
    os.system('sudo ./goofys '+bucket+' /home/chiap')



if __name__ == '__main__':
    try:
        argv = sys.argv[1:]
        for i, options in enumerate(argv):
            if options in ("-bucket","-b","--b"):
                bucket = argv[i+1]
                print('your bucket:',bucket)
                same_command(bucket)
    except Exception as e:
        print(e)
